<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Course extends MY_Controller {
	function __construct(){
			parent::__construct();
			$this->school_id = 0;
			$this->teacher_id = 0;
			$this->load->model('videos');
			$this->load->model('Courses');
		}
	/**
	 * 视频课
	 * @see https://codeigniter.com/user_guide/general/urls.html
	 */
	public function index($status = 10,$sort = 0,$asc_des = 0,$search = 0, $cate_id=0, $kemu=0, $limit = 0)
	{
		if($this->input->post()){
			$limit = 0;
			$post_data = $this->input->post('category');
			$search = $post_data['search'] ? urldecode($post_data['search']) : 0;
			$status = isset($post_data['status']) ? $post_data['status'] : 0;
			$kemu = (isset($post_data['kemu'])) ? $post_data['kemu'] : 0;
			$cate_id = (isset($post_data['cate_id'])) ? $post_data['cate_id'] : 0;
		}else{
			$search = urldecode($search);
			$page_config['uri_segment'] = 9;
		}
		$this->load->model("Courses");
		$data = array( 'status' => $status, 'cate_id' => $cate_id, 'kemu'=>$kemu, 'type' => 1, 'sort' => $sort,'asc_des' => $asc_des,'search' => $search,'limit' => $limit);
		$this->smarty->assign("action", '视频课管理');
		$offset = 10;
		$current_url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
		$result = $this->Courses->get_list_ofpage($data['limit'], $offset,$data);
		//获取app 活动

		/*$this->load->model('activitiess');
		$activity_list = $this->activitiess->get_list(0, 30,  array('type'=>2));*/
		$subject = $this->Courses->get_course_cate_subject($cate_id);
		$cate = $this->Courses->get_cate();
		$this->load->library('pagination');
		$page_config['base_url'] = '/course/index/'.$data['status'].'/'.$data['sort'].'/'.$data['asc_des'].'/'.$data['search'].'/'.$data['cate_id'].'/'.$data['kemu'];
		$page_config['total_rows'] = $result['count'];
		$page_config['per_page'] = $offset;
		$this->pagination->initialize($page_config);
		$pagination = $this->pagination->create_links();
		$this->smarty->assign("wap_url", $this->config->item('wap_url'));
		$this->smarty->assign("current_url", $current_url);
		$this->smarty->assign("data", $data);
		$this->smarty->assign("cate", $cate);
		$this->smarty->assign("zhiboke", $result['result']);
		// $this->smarty->assign("activity_list", $activity_list);
		$this->smarty->assign("pagination", $pagination);
		$this->smarty->assign("subject", $subject);
		$this->smarty->assign("menu", "course");
		$this->smarty->display("course/index.html");
	}

	
	public function edit_shipinke($id){
		$token = get_qiniu_token('VIMG');
		$post_data = $this->input->post();
		if($post_data){
			if($post_data['title'] == ""){
				echo  "标题不能为空";exit;
			}
			$image = $post_data['img_url'];
			if(!empty($image)){
				$post_data['course_img'] = get_qiniu_url($image[0]);
				unset($image[0]);

				if(!empty($image)){
					foreach ($image as $key => $value) {
						$img[] = get_qiniu_url($value);
					}
					$post_data['course_imgs'] = json_encode($img);
				}
			}
			unset($post_data['img_url']);
			unset($post_data['file']);
			$this->db->where('id',$post_data['id']);
			$result = $this->db->update('course',$post_data);
			if($result){
				if($result){
					redirect("/course");
				}
				$data['uid'] = $this->uid;
				$data['cate_id'] = $post_data['cate_id'];
				$data['course_id'] = $this->db->insert_id();
				$data['teacher_id'] = $this->teacher_id;
				$data['school_id'] = $this->school_id;
				$data['type'] = 1;
			}
		}
		$course = $this->Courses->get_course_info($id);
		$course_chapter = $this->Courses->get_chapter_list($id);
		if($course['cate_id'] > 0){
			$cate_subject = $this->Courses->get_course_cate_subject($course['cate_id']);
			$this->smarty->assign("cate_subject", $cate_subject);
		}
		if($course['vid'] > 0){
			$video_info = get_field_by_where("video", "title", "id='{$course['vid']}'");
			$this->smarty->assign("video_title", $video_info['title']);
		}
		$course_imgs = json_decode($course['course_imgs'],true);
		$subject = $this->Courses->get_subject(0);
		$teacher = $this->Courses->get_teacher(0);
		$cate = $this->Courses->get_cate();
		$this->smarty->assign("menu", "course");
		$this->smarty->assign("subject", $subject);
		$this->smarty->assign("cate", $cate);
		$this->smarty->assign("course", $course);
		$this->smarty->assign("chapter", $course_chapter);
		$this->smarty->assign("img_url", $course_imgs);
		$this->smarty->assign("teacher", $teacher);
		$this->smarty->assign("token", $token);
		$this->smarty->assign("post_url", 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
		$this->smarty->display("course/add_video_course.html");
	}

	/**
	 * 发布视频课
	 * @return [type] [description]
	 */
	public function shipinke_publish(){
		$token = get_qiniu_token('VIMG');
		$post_data = $this->input->post();
		if(isset($post_data['img_url'])){
			foreach ($post_data['img_url'] as $key => $value) {
				$post_data['img_url'][$key] = get_qiniu_url($value);
			}
		}
		$this->load->model("Courses");
		if($post_data){
			if($post_data['title'] == ""){
				echo  "标题不能为空";exit;
			}
			$course_id = $this->Courses->publish_course_video(0,0,$post_data);
			header("Location:/course/edit_video_subject/{$course_id}");exit;
		}

		//$subject = $this->Courses->get_subject();
		//$teacher = $this->Courses->get_teacher($this->school_id);
		$cate = $this->Courses->get_cate(1);//录播课分类

		
		//$this->smarty->assign("subject", $subject);
		$this->smarty->assign("cate", $cate);
		
		$this->smarty->assign("token", $token);
		$this->smarty->assign("post_url", "/course/shipinke_publish");
		$this->smarty->display("course/add_video_course.html");
	}

	/**
	 * 安排课程目录
	 * @param  [type] $course_id [description]
	 * @return [type]            [description]
	 */
	public function edit_video_subject($course_id)
	{
		$this->smarty->assign("course_id", $course_id);
		$this->load->model("Courses");
		$course = $this->Courses->get_course_info($course_id);
		if(empty($course)) {
			header("Location:/course");exit;
		}
		$course_chapter = $this->Courses->get_chapter_list($course_id);
		$this->smarty->assign("chapter", $course_chapter);

		$this->smarty->assign("post_url", "/course/shipinke_publish");
		$this->smarty->assign("course", $course);
		
		$this->smarty->display("course/edit_video_subject.html");
	}


	function add_video_chapter(){
		$post_data = $this->input->post();
		if(isset($post_data['id']) && $post_data['id']> 0){
			// $data['name'] = $post_data['name'];
			$this->db->where('id',$post_data['id']);
			$this->db->update('course_chapter',$post_data);
			echo json_encode($post_data);
			exit;
		}

		$result = $this->db->insert('course_chapter',$post_data);
		$res = array("id" => $this->db->insert_id(), "pid" => 0);
		if(!empty($post_data['pid'])){
			$res['pid'] = $post_data['pid'];
		}
		echo json_encode($res);
	}


	function del_video_chapter(){
		$post_data = $this->input->post();
		if(isset($post_data['id']) && $post_data['id'] > 0){
			$this->db->where('id',$post_data['id']);
			$result = $this->db->delete('course_chapter');
			if($result){
				$this->db->where('pid',$post_data['id']);
            	$result = $this->db->delete('course_chapter');
            	if($result){
            		echo 1;exit;
            	}else{
            		echo 0;exit;
            	}
			}else{
				echo 0;exit;
			}
		}
	}



	function video($review = 0,$sort = 0,$asc_des = 0,$search = 0,$zid=0, $limit = 0){
		if($this->input->post()){
			$limit = 0;
			$post_data = $this->input->post('category');
			$search = $post_data['search'] ? urldecode($post_data['search']) : 0;
			$review = isset($post_data['review']) ? $post_data['review'] : 0;
			$zid = (isset($post_data['pid'])) ? $post_data['pid'][0] : 0;
		}else{
			$search = urldecode($search);
			$page_config['uri_segment'] = 11;
		}

		$data = array( 'review' => $review, 'cate_id'=>$zid, 'sort' => $sort,'asc_des' => $asc_des,'search' => $search,'limit' => $limit);
		$this->smarty->assign("action", '视频列表');
		$offset = 10;
		$current_url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
		$result = $this->videos->get_list_ofpage($data['limit'], $offset,$data);

		//获取app 活动

		/*$this->load->model('activitiess');
		$activity_list = $this->activitiess->get_list(0, 30,  array('type'=>2));*/

		$this->load->library('pagination');
		$page_config['base_url'] = '/course/video/'.$data['review'].'/'.$data['sort'].'/'.$data['asc_des'].'/'.$data['search'].'/'.$data['cate_id'];
		$page_config['total_rows'] = $result['count'];
		$page_config['per_page'] = $offset;
		$this->pagination->initialize($page_config);
		$pagination = $this->pagination->create_links();
		$this->smarty->assign("wap_url", $this->config->item('wap_url'));
		$this->smarty->assign("current_url", $current_url);
		$this->smarty->assign("data", $data);
		$this->smarty->assign("video", $result['result']);
		// $this->smarty->assign("activity_list", $activity_list);
		$this->smarty->assign("pagination", $pagination);

		$this->smarty->display("course/video_list.html");
	}

	function select_video($cate_id = 0, $subject_id = 0, $search = 0,$limit = 0){
		if($this->input->post()){
			$limit = 0;
			$post_data = $this->input->post('category');
			$search = !empty($post_data['search']) ? $post_data['search'] : 0;
			$cate_id = !empty($post_data['cate_id']) ? $post_data['cate_id'] : 0;
			$subject_id =  !empty($post_data['subject_id']) ? $post_data['subject_id'] : 0;
		}else{
			$search = urldecode($search);
			$page_config['uri_segment'] = 6;
		}
		$this->load->model("Courses");
		$data = array( 'cate_id' => $cate_id, 'search'=>$search, 'subject_id' => $subject_id);
		$this->smarty->assign("action", '视频列表');
		$offset = 12;
		$current_url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
		$result = $this->Courses->get_video_list_ofpage($limit, $offset, $data);

		$this->load->library('pagination');
		$page_config['base_url'] = '/course/select_video/'.$cate_id.'/'.$subject_id.'/'.$search;
		$page_config['total_rows'] = $result['count'];
		$page_config['per_page'] = $offset;
		$this->pagination->initialize($page_config);
		$pagination = $this->pagination->create_links();
		$this->smarty->assign("wap_url", $this->config->item('wap_url'));
		$this->smarty->assign("current_url", $current_url);
		$this->smarty->assign("data", $data);
		$this->smarty->assign("page_total", ceil($result['count']/$offset));
		
		$this->smarty->assign("video", $result['result']);

		$cate = $this->Courses->get_cate();
		$subject = $this->Courses->get_course_cate_subject($cate_id);
		
		$this->smarty->assign("cate", $cate);
		$this->smarty->assign("subject", $subject);
		// $this->smarty->assign("activity_list", $activity_list);
		$this->smarty->assign("pagination", $pagination);

		$this->smarty->display("course/select_video_list.html");
	}


	function add_video(){
		$post_data = $this->input->post();
		if(isset($post_data['id']) && $post_data['id'] > 0){

		}
		$token = get_qiniu_token('VIMG');
		//if(isset($post_data['image_url'])) $post_data['image_url'] = get_qiniu_url($data['image_url']);
		unset($post_data['id']);
		if(isset($post_data['title']) && $post_data['title'] == ""){
			echo "请输入标题";exit;
		}
		if(isset($post_data['description']) && $post_data['description'] == ""){
			echo "请视频描述";exit;
		}
		if(isset($post_data['description']) && isset($post_data['title'])){
			$post_data['add_time'] = time();
			$post_data['school_id'] = $this->school_id;
			$post_data['teacher_id'] = $this->teacher_id;
			$result = $this->db->insert('video',$post_data);
		}
		$this->smarty->assign("token", $token);
		$this->smarty->assign("post_url", "/course/add_video");
		$this->smarty->display("course/add_video.html");
	}

	function edit_video($id = 0){
		$id = intval($id);

		if($id > 0 ){
			$token = get_qiniu_token('VIMG');
			$videos_info = $this->videos->get_video_info($id);
			$this->smarty->assign("token", $token);
			$this->smarty->assign("videos_info", $videos_info[0]);
			$this->smarty->assign("post_url", 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
			$this->smarty->display("course/add_video.html");
		}
		$post_data = $this->input->post();
		if(isset($post_data['id']) && $post_data['id'] > 0){
			$post_data['edit_time'] = time();
			if(isset($post_data['image_url']) && $post_data['image_url'] != ''){
				if(isset($post_data['image_url'])) $post_data['image_url'] = get_qiniu_url($post_data['image_url']);
			}
			$this->db->where('id', $post_data['id']);
			$result = $this->db->update('video', $post_data);
			if($result){
				//show_message($_SERVER['HTTP_REFERER'], "视频修改成功");exit;
			}else{
				//show_message($_SERVER['HTTP_REFERER'], "视频修改失败");exit;
			}
		}
	}

	/**
	 * 直播list
	 * @param  integer $status  [description]
	 * @param  integer $sort    [description]
	 * @param  integer $asc_des [description]
	 * @param  integer $search  [description]
	 * @param  integer $kemu    [description]
	 * @param  integer $limit   [description]
	 * @return [type]           [description]
	 */
	function zhiboke($status = 10,$sort = 0,$asc_des = 0,$search = 0,$kemu=0, $limit = 0){
		if($this->input->post()){
			$limit = 0;
			$post_data = $this->input->post('category');
			$search = $post_data['search'] ? urldecode($post_data['search']) : 0;
			$review = isset($post_data['status']) ? $post_data['status'] : 0;
			$kemu = (isset($post_data['kemu'])) ? $post_data['kemu'][0] : 0;
		}else{
			$search = urldecode($search);
			$page_config['uri_segment'] = 8;
		}
		$this->load->model("Courses");
		$data = array( 'status' => $status, 'kemu'=>$kemu, 'type' => 2, 'sort' => $sort,'asc_des' => $asc_des,'search' => $search,'limit' => $limit);
		$this->smarty->assign("action", '视频列表');
		$offset = 1;
		$current_url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
		$this->load->model('Anchors');
		$result = $this->Anchors->get_list_ofpage($data['limit'], $offset,$data);
		//获取app 活动

		/*$this->load->model('activitiess');
		$activity_list = $this->activitiess->get_list(0, 30,  array('type'=>2));*/
		$subject = $this->Courses->get_subject();
		$this->load->library('pagination');
		$page_config['base_url'] = '/course/zhiboke/'.$data['status'].'/'.$data['sort'].'/'.$data['asc_des'].'/'.$data['search'].'/'.$data['kemu'];
		$page_config['total_rows'] = $result['count'];
		$page_config['per_page'] = $offset;
		$this->pagination->initialize($page_config);
		$pagination = $this->pagination->create_links();
		$this->smarty->assign("wap_url", $this->config->item('wap_url'));
		$this->smarty->assign("current_url", $current_url);
		$this->smarty->assign("data", $data);
		$this->smarty->assign("zhiboke", $result['result']);
		// $this->smarty->assign("activity_list", $activity_list);
		$this->smarty->assign("menu", "course");
		$this->smarty->assign("pagination", $pagination);
		$this->smarty->assign("subject", $subject);
		$this->smarty->display("course/course_zhiboke_list.html");
	}

	public function edit_zhiboke($id = 0){
		$id = intval($id);
		if($id > 0){
			$this->load->model('Anchors');
			$this->load->model("Courses");
			$token = get_qiniu_token('VIMG');
			$post_data = $this->input->post();
			if(isset($post_data['id']) && $post_data['id'] > 0){
				$post_data['start_time'] = strtotime($post_data['start_time']);
				$post_data['end_time'] = strtotime($post_data['end_time']);
				if(isset($post_data['img_url'])){
					$image = $post_data['img_url'];
					if(!empty($image)){
						$post_data['course_img'] = $image[0];
						unset($image[0]);
						if(!empty($image)){
							$post_data['course_imgs'] = json_encode($image);
						}
					}
					unset($post_data['img_url']);
				}

				$this->db->where('id', $post_data['id']);
				$result = $this->db->update('course',$post_data);
			}
			$result = $this->Anchors->get_zhiboke_info($id);
			$subject = $this->Courses->get_subject();
			$teacher = $this->Courses->get_teacher($this->school_id);
			$img_url = json_decode($result['course_imgs']);
			$this->smarty->assign("subject", $subject);
			$this->smarty->assign("teacher", $teacher);
			$this->smarty->assign("info", $result);
			$this->smarty->assign("img_url", $img_url);
			$this->smarty->assign("token", $token);
			//$this->smarty->assign("post_url", "/course/edit_zhiboke");
			$this->smarty->assign("post_url", 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
			$this->smarty->display("course/add_zhibo_course.html");

		}
	}

	/**
	 * 发布直播
	 * @return [type] [description]
	 */
	public function zhiboke_publish(){
		$token = get_qiniu_token('VIMG');
		$post_data = $this->input->post();
		if($post_data){

			if($post_data['title'] == ""){
				echo  "标题不能为空";exit;
			}
			$post_data['school_id'] = $this->school_id;
			$post_data['teacher_id'] = $this->teacher_id;
			$post_data['add_time'] = time();
			$post_data['type'] = 2;
			$post_data['start_time'] = strtotime($post_data['start_time']);
			$post_data['end_time'] = strtotime($post_data['end_time']);
			$image = $post_data['img_url'];
			if(!empty($image)){
				$post_data['course_img'] = $image[0];
				unset($image[0]);
				if(!empty($image)){
					$post_data['course_imgs'] = json_encode($image);
				}
			}
			unset($post_data['img_url']);
			unset($post_data['id']);
			$result = $this->db->insert('course',$post_data);
			if($result){

			}
		}
		$this->load->model("Courses");
		$subject = $this->Courses->get_subject();
		$teacher = $this->Courses->get_teacher($this->school_id);
		$this->smarty->assign("menu", "course");
		$this->smarty->assign("subject", $subject);
		$this->smarty->assign("teacher", $teacher);
		$this->smarty->assign("token", $token);
		$this->smarty->assign("post_url", "/course/zhiboke_publish");
		$this->smarty->display("course/add_zhibo_course.html");
	}

	/**
	 * 视频课分类管理
	 * @param  integer $search [description]
	 * @param  integer $limit  [description]
	 * @return [type]          [description]
	 */
	function cate_list($search = 0, $limit = 0){
		$offset = 10;
		if($this->input->post()){
			$limit = 0;
			$post_data = $this->input->post('category');
			$search = $post_data['search'] ? urldecode($post_data['search']) : 0;
		}else{
			$search = urldecode($search);
			$page_config['uri_segment'] = 4;
		}
		$this->load->model("Courses");
		$list = $this->Courses->get_cate_list($search,$offset,$limit);
		$data = array( 'search' => $search,'limit' => $limit);
		$this->smarty->assign("action", '视频课分类管理');
		
		$current_url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
		$this->load->library('pagination');
		$page_config['base_url'] = '/course/cate_list/'.$data['search'];
		$page_config['total_rows'] = $list['count'];
		$page_config['per_page'] = $offset;
		$this->pagination->initialize($page_config);
		$pagination = $this->pagination->create_links();
		$this->smarty->assign("current_url", $current_url);

		$this->smarty->assign("pagination", $pagination);

		$this->smarty->assign("data", $data);
		$this->smarty->assign("menu", "course");
		$this->smarty->assign("list", $list['result']);
		$this->smarty->display("course/course_cate_list.html");
	}

	/**
	 * 添加视频课分类
	 * @return [type] [description]
	 */
	function cate_publish(){
		$this->smarty->assign("action", '添加视频课分类');
		$post_data = $this->input->post();
		if($post_data){
			unset($post_data['id']);
			if($post_data['name'] == ""){
				echo  "标题不能为空";exit;
			}
			if($post_data['sort'] == ""){
				echo  "排序不能为空";exit;
			}
			$post_data['school_id'] = 0;
			$post_data['teacher_id'] = 0;
			$post_data['add_time'] = time();
			$post_data['type'] = 1;
			$result = $this->db->insert('course_cate',$post_data);
			if($result){
				redirect("/course/cate_list");
			}
		}
		$this->load->model("Courses");
		$subject = $this->Courses->get_subject();
		$teacher = $this->Courses->get_teacher();
		$this->smarty->assign("menu", "course");
		$this->smarty->assign("subject", $subject);
		$this->smarty->assign("teacher", $teacher);
		$this->smarty->assign("post_url", "/course/cate_publish");
		$this->smarty->display("course/add_course_cate.html");
	}

	/**
	 * 编辑视频课分类
	 * @param  integer $id [description]
	 * @return [type]      [description]
	 */
	public function edit_course_cate($id = 0){
		$id = intval($id);
		if($id > 0){
			$this->smarty->assign("action", '编辑视频课分类');
			$this->load->model("Courses");
			$post_data = $this->input->post();
			if(isset($post_data['id']) && $post_data['id'] > 0){
				$this->db->where('id', $post_data['id']);
				$result = $this->db->update('course_cate',$post_data);
				if($result){
					redirect("/course/cate_list");
				}
			}
			$result = $this->Courses->get_cate_info($id);
			$this->smarty->assign("info", $result);
			$this->smarty->assign("post_url", 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
			$this->smarty->display("course/add_course_cate.html");

		}
	}

	/**
	 * 编辑视频课程科目
	 * @param  integer $id [description]
	 * @return [type]      [description]
	 */
	public function edit_course_cate_subject($id = 0){
		$id = intval($id);
		$this->smarty->assign("action", '编辑视频课程科目');
		if($id > 0){
			$this->load->model("Courses");
			$post_data = $this->input->post();
			if(isset($post_data['id']) && $post_data['id'] > 0){
				$this->db->where('id', $post_data['id']);
				$result = $this->db->update('course_cate_subject',$post_data);
				if($result){
					redirect("/course/cate_subjects_list");
				}
			}
			$result = $this->Courses->get_cate_subject_info($id);
			$cate = $this->Courses->get_cate();
			$this->smarty->assign("info", $result);
			$this->smarty->assign("cate", $cate);
			$this->smarty->assign("post_url", 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
			$this->smarty->display("course/add_course_cate_subject.html");

		}
	}

	/**
	 * 添加视频课程科目
	 * @return [type] [description]
	 */
	function cate_subject_publish(){
		$this->smarty->assign("action", '添加视频课程科目');
		$post_data = $this->input->post();
		if($post_data){
			unset($post_data['id']);
			if($post_data['name'] == ""){
				echo  "标题不能为空";exit;
			}
			if($post_data['sort'] == ""){
				echo  "排序不能为空";exit;
			}
			$post_data['type'] = 1;
			$post_data['school_id'] = 0;
			$post_data['teacher_id'] = 0;
			$post_data['add_time'] = time();
			$result = $this->db->insert('course_cate_subject',$post_data);
			if($result){
				redirect("/course/cate_subjects_list");
			}
		}
		$this->load->model("Courses");
		$subject = $this->Courses->get_subject();
		$teacher = $this->Courses->get_teacher();
		$cate = $this->Courses->get_cate();
		$this->smarty->assign("menu", "course");
		$this->smarty->assign("cate", $cate);
		$this->smarty->assign("subject", $subject);
		$this->smarty->assign("teacher", $teacher);
		$this->smarty->assign("post_url", "/course/cate_subject_publish");
		$this->smarty->display("course/add_course_cate_subject.html");
	}


	/**
	 * 视频课科目管理
	 * @param  integer $search [description]
	 * @param  integer $limit  [description]
	 * @return [type]          [description]
	 */
	function cate_subjects_list($search = 0, $limit = 0){
		$offset = 10;
		if($this->input->post()){
			$limit = 0;
			$post_data = $this->input->post('category');
			$search = $post_data['search'] ? urldecode($post_data['search']) : 0;
		}else{
			$search = urldecode($search);
			$page_config['uri_segment'] = 4;
		}
		$this->load->model("Courses");
		$list = $this->Courses->get_cate_subjects_list($search,$offset,$limit);
		$data = array( 'search' => $search,'limit' => $limit);
		$this->smarty->assign("action", '视频课科目管理');
		
		$current_url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
		$this->load->library('pagination');
		$page_config['base_url'] = '/course/cate_subjects_list/'.$data['search'];
		$page_config['total_rows'] = $list['count'];
		$page_config['per_page'] = $offset;
		$this->pagination->initialize($page_config);
		$pagination = $this->pagination->create_links();
		$this->smarty->assign("current_url", $current_url);

		$this->smarty->assign("pagination", $pagination);

		$this->smarty->assign("data", $data);
		$this->smarty->assign("menu", "course");
		$this->smarty->assign("list", $list['result']);
		$this->smarty->display("course/course_subjects_list.html");
	}

	function get_cate_subject(){
		$post_data = $this->input->post();
		$result = $this->Courses->get_course_cate_subject($post_data['id']);
		echo json_encode($result);
	}

	function delete_course_cate_subject($id){
		if($id > 0){
			$this->db->where('id',$id);
			$result = $this->db->delete('course_cate_subject');
			if($result){
				redirect("/course/cate_subjects_list");
			}
		}

	}

	function delete_course_cate($id){
		if($id > 0){
			$this->db->where('id',$id);
			$result = $this->db->delete('course_cate');
			if($result){
				redirect("/course/cate_list");
			}
		}

	}

	function delete_shipinke($ids = 0){
		if($_POST){
			$ids = $this->input->post('ids');
		}else{
			$ids = (array) $ids;
		}
		delete_data($ids, "course");
		if($this->db->affected_rows() > 0){
			show_message($_SERVER['HTTP_REFERER'], "删除成功");
		}else{
			show_message($_SERVER['HTTP_REFERER'], "删除失败，请重试！");
		}
	}

	function change_status(){
		$post_data = $this->input->post();
		if(empty($post_data['course_id'])){
			echo json_encode(array("error" => 1, "msg" => "缺少课程ID"));
			exit;
		}
		$this->db->where("id", $post_data['course_id']);
		$this->db->update("course", array("status" => $post_data['status']));
		if($this->db->affected_rows() > 0){
			echo json_encode(array("error" => 0, "msg" => "修改成功"));
			exit;
		}else{
			echo json_encode(array("error" => 1, "msg" => "修改失败，请重试"));
			exit;
		}
	}
}
